﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

using FanaticToy.Core.IdentifierStrategy;
using FanaticToy.Core.Utils;

namespace FanaticToy.Core.Configure
{
    ///<summary>策略</summary>
    public class FileSystemLevel2FormatToyStrategy : BaseToyStrategy
    {
        ///<summary>第二项的起始裁切列表。</summary>
        public List<string> StartFlagList { get; set; } = new List<string>();

        ///<summary>第二项的结束裁切列表。</summary>
        public List<string> EndFlagList { get; set; } = new List<string>();

        public override bool IsMatched(string path)
        {
            var fileName = Path.GetFileNameWithoutExtension(path);

            var isMatched = PathUtils.IsFileNameMatched(fileName, StartFlagList, EndFlagList);

            return isMatched;
        }

        public override IEnumerable<string> GetAvaliablePaths(string basePath)
        {
            return Directory.GetDirectories(basePath).SelectMany(sd => Directory.GetFiles(sd));
        }

        public override CodeMetaData GetCodeMetaData(string filePath, string fileId, string memberId)
        {
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var isMatched = PathUtils.IsFileNameWithIdMatched(fileName, memberId, StartFlagList, EndFlagList);

            if (isMatched == false) return null;

            var codeMetaData = new CodeMetaData();
            codeMetaData.FilePath = filePath;
            codeMetaData.LineId = 0;

            return codeMetaData;
        }

        public override LocationIdentifier GetLocationIdentifier(string filePath, int lineId)
        {
            if (IsMatched(filePath) == false) return null;
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var memberId = PathUtils.GetTrimedResult(fileName, StartFlagList, EndFlagList);
            var dirPath = Path.GetDirectoryName(filePath);
            var classId = Path.GetFileNameWithoutExtension(dirPath);

            return new LocationIdentifier() { FileId = classId, MemberId = memberId };
        }

    }
}
